/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.headerfooter;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.reportgear.core.api.BaseObject;
import com.reportgear.core.api.XmlSerializerWithReadReturn;
import com.reportgear.core.util.BaseUtils;
import com.sot.core.util.NumberUtils;

/**
 * 报表页脚页眉-核心信息
 * 
 * @version 1.0 2011-4-23
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ReportHFCore extends BaseObject implements XmlSerializerWithReadReturn<ReportHFCore> {
	public static final String XML_TAG_HEADER = "header";
	public static final String XML_TAG_FOOTER = "footer";

	private List<HFElement> leftList = new ArrayList<HFElement>();
	private List<HFElement> centerList = new ArrayList<HFElement>();
	private List<HFElement> rightList = new ArrayList<HFElement>();

	private double height;

	public List<HFElement> getLeftList() {
		return this.leftList;
	}

	public void setLeftList(List<HFElement> leftList) {
		this.leftList = leftList;
	}

	public List<HFElement> getCenterList() {
		return this.centerList;
	}

	public void setCenterList(List<HFElement> centerList) {
		this.centerList = centerList;
	}

	public List<HFElement> getRightList() {
		return this.rightList;
	}

	public void setRightList(List<HFElement> rightList) {
		this.rightList = rightList;
	}

	@Override
	public ReportHFCore clone() {
		ReportHFCore result = (ReportHFCore) super.clone();

		result.leftList = BaseUtils.copyList(this.leftList);
		result.centerList = BaseUtils.copyList(this.centerList);
		result.rightList = BaseUtils.copyList(this.rightList);

		return result;
	}

	/**
	 * 设置高度
	 * 
	 * @param height
	 *            高度
	 */
	public void setHeight(double height) {
		this.height = height;
	}

	/**
	 * 获取高度
	 * 
	 * @return 高度
	 */
	public double getHeight() {
		return height;
	}

	@Override
	public ReportHFCore read(Element parent) {
		if (parent == null) {
			return this;
		}

		this.height = NumberUtils.convertToDouble(parent.elementTextTrim("height"), 0);

		this.readList(parent, "leftList", this.leftList);
		this.readList(parent, "centerList", this.centerList);
		this.readList(parent, "rightList", this.rightList);

		return this;
	}

	// 读取List
	private void readList(Element parent, String nodeTag, List<HFElement> list) {
		Element el;
		el = parent.element(nodeTag);
		list.clear();
		if (el != null) {
			for (Object o : el.elements()) {
				Element ele = (Element) o;
				String type = ele.elementTextTrim(HFElement.XML_TAG_TYPE);
				HFElement element = HFElementFactory.createElement(type);
				if (element != null) {
					list.add(element.read(ele));
				}
			}
		}
	}

	@Override
	public void write(Element parent) {
		Element el;

		el = parent.addElement("height");
		el.addText(String.valueOf(this.height));

		el = parent.addElement("leftList");
		for (HFElement element : this.leftList) {
			Element child = el.addElement("element");
			element.write(child);
		}

		el = parent.addElement("centerList");
		for (HFElement element : this.centerList) {
			Element child = el.addElement("element");
			element.write(child);
		}

		el = parent.addElement("rightList");
		for (HFElement element : this.rightList) {
			Element child = el.addElement("element");
			element.write(child);
		}
	}
}